﻿namespace AC.ExtendedRenderer.Toolkit
{
    using ComponentFactory.Krypton.Toolkit;
    using System;
    using System.Drawing;
    using System.Drawing.Drawing2D;
    using System.Windows.Forms;

    [ToolboxBitmap(typeof(DataGridView))]
    public class KryptonGrid : DataGridView
    {
        private IDisposable _mementoBack;
        private IPalette _palette;
        private PaletteBackInheritRedirect _paletteBack;
        private PaletteBorderInheritRedirect _paletteBorder;
        private PaletteContentInheritRedirect _paletteContent;
        private PaletteRedirect _paletteRedirect;

        public KryptonGrid()
        {
            base.SetStyle(ControlStyles.OptimizedDoubleBuffer | ControlStyles.AllPaintingInWmPaint | ControlStyles.ResizeRedraw, true);
            this._palette = KryptonManager.CurrentGlobalPalette;
            if (this._palette != null)
            {
                this._palette.PalettePaint += new EventHandler<PaletteLayoutEventArgs>(this.OnPalettePaint);
            }
            KryptonManager.GlobalPaletteChanged += new System.EventHandler(this.OnGlobalPaletteChanged);
            this._paletteRedirect = new PaletteRedirect(this._palette);
            this._paletteBack = new PaletteBackInheritRedirect(this._paletteRedirect);
            this._paletteBorder = new PaletteBorderInheritRedirect(this._paletteRedirect);
            this._paletteContent = new PaletteContentInheritRedirect(this._paletteRedirect);
            base.RowTemplate.DefaultCellStyle.SelectionBackColor = Color.Transparent;
            base.DefaultCellStyle.SelectionBackColor = Color.Transparent;
            base.DefaultCellStyle.SelectionForeColor = Color.Navy;
            base.RowTemplate.DefaultCellStyle.SelectionForeColor = Color.Navy;
            base.AlternatingRowsDefaultCellStyle.BackColor = this._palette.ColorTable.ToolStripGradientBegin;
        }

        protected override void Dispose(bool disposing)
        {
            if (disposing)
            {
                if (this._mementoBack != null)
                {
                    this._mementoBack.Dispose();
                    this._mementoBack = null;
                }
                if (this._palette != null)
                {
                    this._palette.PalettePaint -= new EventHandler<PaletteLayoutEventArgs>(this.OnPalettePaint);
                    this._palette = null;
                }
                KryptonManager.GlobalPaletteChanged -= new System.EventHandler(this.OnGlobalPaletteChanged);
            }
            base.Dispose(disposing);
        }

        protected override void OnCellPainting(DataGridViewCellPaintingEventArgs e)
        {
            if ((e.RowIndex == -1) && (e.ColumnIndex != -1))
            {
                using (GraphicsPath path = new GraphicsPath())
                {
                    IRenderer renderer = this._palette.GetRenderer();
                    Rectangle rect = new Rectangle(e.CellBounds.X, e.CellBounds.Y, e.CellBounds.Width, e.CellBounds.Height);
                    Rectangle rectangle2 = new Rectangle(e.CellBounds.X - 1, e.CellBounds.Y - 1, e.CellBounds.Width + 2, e.CellBounds.Height + 1);
                    path.AddRectangle(rect);
                    Color toolStripGradientBegin = this._palette.ColorTable.ToolStripGradientBegin;
                    Color menuStripGradientEnd = this._palette.ColorTable.MenuStripGradientEnd;
                    using (RenderContext context = new RenderContext(this, e.Graphics, rect, renderer))
                    {
                        if (e.State == DataGridViewElementStates.Selected)
                        {
                            this._paletteBack.Style = PaletteBackStyle.HeaderPrimary;
                            this._paletteBorder.Style = PaletteBorderStyle.HeaderPrimary;
                            this._paletteContent.Style = PaletteContentStyle.HeaderPrimary;
                        }
                        else
                        {
                            this._paletteBack.Style = PaletteBackStyle.HeaderPrimary;
                            this._paletteBorder.Style = PaletteBorderStyle.HeaderPrimary;
                            this._paletteContent.Style = PaletteContentStyle.HeaderPrimary;
                        }
                        e.Graphics.FillRectangle(new SolidBrush(Color.White), rect);
                        using (LinearGradientBrush brush = new LinearGradientBrush(rectangle2, toolStripGradientBegin, menuStripGradientEnd, LinearGradientMode.Vertical))
                        {
                            e.Graphics.FillRectangle(brush, rect);
                        }
                        renderer.RenderStandardBorder.DrawBorder(context, new Rectangle(e.CellBounds.X - 1, e.CellBounds.Y - 1, e.CellBounds.Width + 2, e.CellBounds.Height + 1), this._paletteBorder, VisualOrientation.Top, PaletteState.Normal);
                    }
                }
                e.Paint(e.CellBounds, DataGridViewPaintParts.ContentForeground | DataGridViewPaintParts.ContentBackground | DataGridViewPaintParts.Border);
                e.Handled = true;
            }
            else if (((e.RowIndex >= 0) && (e.ColumnIndex > -1)) && ((e.State & DataGridViewElementStates.Selected) == DataGridViewElementStates.Selected))
            {
                using (GraphicsPath path2 = new GraphicsPath())
                {
                    IRenderer renderer2 = this._palette.GetRenderer();
                    path2.AddRectangle(new Rectangle(e.CellBounds.X, e.CellBounds.Y, e.CellBounds.Width, e.CellBounds.Height));
                    using (RenderContext context2 = new RenderContext(this, e.Graphics, e.ClipBounds, renderer2))
                    {
                        this._paletteBack.Style = PaletteBackStyle.ButtonListItem;
                        this._paletteBorder.Style = PaletteBorderStyle.ButtonNavigatorStack;
                        this._mementoBack = renderer2.RenderStandardBack.DrawBack(context2, new Rectangle(e.CellBounds.X, e.CellBounds.Y, e.CellBounds.Width, e.CellBounds.Height), path2, this._paletteBack, VisualOrientation.Top, PaletteState.Tracking, this._mementoBack);
                        renderer2.RenderStandardBorder.DrawBorder(context2, new Rectangle(e.CellBounds.X, e.CellBounds.Y, e.CellBounds.Width, e.CellBounds.Height), this._paletteBorder, VisualOrientation.Top, PaletteState.Normal);
                    }
                }
                e.Paint(e.CellBounds, DataGridViewPaintParts.ContentForeground | DataGridViewPaintParts.ContentBackground | DataGridViewPaintParts.Border);
                e.Handled = true;
            }
            else if (e.ColumnIndex == -1)
            {
                using (GraphicsPath path3 = new GraphicsPath())
                {
                    IRenderer renderer3 = this._palette.GetRenderer();
                    path3.AddRectangle(new Rectangle(e.CellBounds.X + 1, e.CellBounds.Y, e.CellBounds.Width, e.CellBounds.Height));
                    Color color3 = this._palette.ColorTable.ToolStripGradientBegin;
                    Color color4 = this._palette.ColorTable.MenuStripGradientEnd;
                    using (RenderContext context3 = new RenderContext(this, e.Graphics, e.ClipBounds, renderer3))
                    {
                        if (e.State == DataGridViewElementStates.Selected)
                        {
                            this._paletteBack.Style = PaletteBackStyle.HeaderPrimary;
                            this._paletteBorder.Style = PaletteBorderStyle.HeaderPrimary;
                        }
                        else
                        {
                            this._paletteBack.Style = PaletteBackStyle.HeaderPrimary;
                            this._paletteBorder.Style = PaletteBorderStyle.HeaderPrimary;
                        }
                        renderer3.RenderStandardBorder.DrawBorder(context3, new Rectangle(e.CellBounds.X, e.CellBounds.Y - 1, e.CellBounds.Width, e.CellBounds.Height + 1), this._paletteBorder, VisualOrientation.Top, PaletteState.Normal);
                        e.Graphics.FillRectangle(new SolidBrush(Color.White), e.CellBounds);
                        using (LinearGradientBrush brush2 = new LinearGradientBrush(e.CellBounds, color3, color4, LinearGradientMode.Vertical))
                        {
                            e.Graphics.FillRectangle(brush2, e.CellBounds);
                        }
                    }
                }
                e.Paint(new Rectangle(e.CellBounds.X + 1, e.CellBounds.Y, e.CellBounds.Width, e.CellBounds.Height - 1), DataGridViewPaintParts.ContentForeground | DataGridViewPaintParts.ContentBackground | DataGridViewPaintParts.Border);
                e.Handled = true;
            }
            else
            {
                base.OnCellPainting(e);
            }
        }

        private void OnGlobalPaletteChanged(object sender, EventArgs e)
        {
            if (this._palette != null)
            {
                this._palette.PalettePaint -= new EventHandler<PaletteLayoutEventArgs>(this.OnPalettePaint);
            }
            this._palette = KryptonManager.CurrentGlobalPalette;
            this._paletteRedirect.Target = this._palette;
            if (this._palette != null)
            {
                this._palette.PalettePaint += new EventHandler<PaletteLayoutEventArgs>(this.OnPalettePaint);
                base.AlternatingRowsDefaultCellStyle.BackColor = this._palette.ColorTable.ToolStripGradientBegin;
            }
            base.Invalidate();
        }

        private void OnPalettePaint(object sender, PaletteLayoutEventArgs e)
        {
            base.Invalidate();
        }

        protected override void OnRowPrePaint(DataGridViewRowPrePaintEventArgs e)
        {
            e.PaintParts &= ~DataGridViewPaintParts.Focus;
            Rectangle rect = new Rectangle(base.RowHeadersWidth, e.RowBounds.Top, (base.Columns.GetColumnsWidth(DataGridViewElementStates.Visible) - base.HorizontalScrollingOffset) + 1, e.RowBounds.Height);
            if ((e.State & DataGridViewElementStates.Selected) == DataGridViewElementStates.Selected)
            {
                using (GraphicsPath path = new GraphicsPath())
                {
                    IRenderer renderer = this._palette.GetRenderer();
                    path.AddRectangle(rect);
                    using (RenderContext context = new RenderContext(this, e.Graphics, rect, renderer))
                    {
                        this._paletteBack.Style = PaletteBackStyle.ButtonNavigatorStack;
                        this._paletteBorder.Style = PaletteBorderStyle.ButtonNavigatorStack;
                        this._mementoBack = renderer.RenderStandardBack.DrawBack(context, rect, path, this._paletteBack, VisualOrientation.Top, PaletteState.Tracking, this._mementoBack);
                        renderer.RenderStandardBorder.DrawBorder(context, rect, this._paletteBorder, VisualOrientation.Top, PaletteState.Normal);
                    }
                }
            }
        }
    }
}

